package com.ml.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ml.constant.BaseResult;
import com.ml.dto.OrderParam;
import com.ml.enums.EnumPayStatus;
import com.ml.exception.ExchangeAssert;
import com.ml.exception.ExchangeError;
import com.ml.feign.SettingRemote;
import com.ml.message.OrderSuccessVO;
import com.ml.model.Symbol;
import com.ml.service.OrderFactory;
import com.ml.service.StateHandler;
import com.ml.util.DataUtil;
import com.virgil.core.annotation.ApiLog;
import com.virgil.core.annotation.Version;
import com.virgil.core.controller.BaseController;

/**
 * @author Virgil
 * @Date 2020年8月20日
 * @Describe
 *           <p>
 *           订单
 *           </p>
 */
@Version(1)
@RestController
@RequestMapping("/{version}/exchange/order")
public class OrderController extends BaseController{

	@Autowired
	SettingRemote settingRemote;
	
	/**
	 * 币币交易下单
	 * @param param
	 * @return
	 */
	@PostMapping("/addNewOrder")
	@ApiLog
	public BaseResult<OrderSuccessVO> addNewOrder(@RequestBody OrderParam param) {
		// 获取交易对配置信息
		List<Symbol> symbols = settingRemote.getSymbols();
		Symbol symbol = symbols.parallelStream().filter(v -> v.getSymbol().equals(param.getSymbol())).findFirst().orElse(null);
		if (symbol == null) {
			ExchangeAssert.createException(ExchangeError.Arg, "未开启交易对");
		}
		if (symbol.getState()!=1) {
			ExchangeAssert.createException(ExchangeError.Arg, "交易对未开启交易");
		}
		// 校验订单是否合法
		OrderSuccessVO vo = DataUtil.checkParam(param, symbol);
		vo.setUid(getUserId());
		// 推送消息
		StateHandler stateHandler = OrderFactory.getByOrderState(EnumPayStatus.UNPAID);
		vo = stateHandler.handler(vo);
		return new BaseResult<OrderSuccessVO>().success(vo);
	}

//    /**
//     * @param param
//     * @return void 返回类型
//     * @throws
//     * @Title: addNewOrder
//     * @Description: TODO(添加新的订单)
//     */
//    @PostMapping("/cancelOrder")
//    public BaseResult<Object> cancelOrder(@RequestBody CancelOrderParam param) {
//        BaseResult<Object> res = new BaseResult<Object>();
//        ValidationResult vr = ValidationUtils.validateEntity(param);
//        if (vr.isHasErrors()) {
//            return res.error(vr.getFirstErrors());
//        }
//        Order order = new Order();
//        order.setId(param.getId());
//        order.setCoinTeam(param.getCoinTeam());
//        //实现-查看是否是可以撤销的状态
//        return res.success("操作成功！");
//    }

//	/**
//	 * @Title: addNewOrder
//	 * @Description: TODO(添加新的订单)
//	 * @param  参数
//	 * @return void 返回类型
//	 * @throws
//	 */
//	@Profile("local")
//	@PostMapping("/addNewOrders/{size}")
//	public JsonResult<Order> addNewOrders(@PathVariable("size") long size) {
//		try {
//			JsonResult<Order> res=new JsonResult<Order>();
//			for (long i = 1; i <= size; i++) {
//				Random random = new Random();
//				int price = random.nextInt(100)+1;
//				OrderBO param = new OrderBO(Boolean.TRUE, new BigDecimal(price), BigDecimal.ONE, BigDecimal.ONE, "XBIT-USDT", Boolean.FALSE);
//				Order order = DataUtil.paramToOrder(param);
//				order.setId(SnowflakeIdWorker.generateId());
//				order.setUid(i);
//				StateHandler stateHandler = OrderFactory.getByOrderState(OrderState.PAID);
//				stateHandler.handler(order);
//			}
//			return res.success("成功:"+size);
//		} catch (Exception e) {
//			log.error("添加新的订单错误："+e);
//			e.printStackTrace();
//			return new JsonResult<Order>(e);
//		}
//	}
}
